<template>
	<view style="padding: 30px;">
		<view style="margin-top: 10px;display: flex; justify-content: center;">
			<image :src="iconFilePath" @click="chooseImage()" class="tx">
			</image>
		</view>
		<button @click="downloadImage()">下载网络图片</button>
		<view>
			<button @click="getweather()">天气预报</button>
			<view style="margin: 20rpx; padding: 20rpx; background: #f5f5f5; border-radius: 10rpx;"
				v-if="weatherResult">
				<view style="font-size: 28rpx; line-height: 48rpx;">
					<view>城市：{{ weatherResult.city }}</view>
					<view>日期：{{ weatherResult.date }}</view>
					<view>天气：{{ weatherResult.type }}</view>
					<view>温度：{{ weatherResult.tempRange }}</view>
				</view>
			</view>
		</view>
		<navigator url="/pages/APIpages/LogPages/LogPages"><button>打印日志</button></navigator>
		<!---uni.navigateTo-->
		<navigator url="/pages/APIpages/TimerPage/TimerPage" open-type="redirect"><button>计数器</button></navigator>
		<button @click="goStoPage()">数据缓存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				iconFilePath: "/static/logo.png",
				weatherResult: null,
				imageUrl: "https://cdn.pixabay.com/photo/2025/11/05/20/57/monastery-9939590_1280.jpg",
				lastProgress: -1

			}
		},
		onLoad() {
			uni.preloadPage({
				url: "/pages/APIpages/StoragePage/StoragePage"
			})
		},
		methods: {
			goStoPage() {
				uni.reLaunch({
					url: "/pages/APIpages/StoragePage/StoragePage",
					success() {
						console.log("navigateTo-->success");
					},
					fail(e) {
						console.log("navigateTo-->fail:" + JSON.stringify(e));
					},
					complete() {
						console.log("navigateTo-->complete");
					}
				})
				//uni.redirectTo({
				//url:"/pages/APIpages/StoragePage/StoragePage"
				//})
				//uni.switchTab跳转到tabar页面
			},
			chooseImage() {
				uni.chooseImage({
					count: 6,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: (res) => {
						console.log("选择的图片路径:", res.tempFilePaths);
						this.iconFilePath = res.tempFilePaths[0];
						uni.previewImage({
							urls: res.tempFilePaths,
						})
					}
				});
			},
			getweather() {
				uni.request({
					url: 'http://t.weather.sojson.com/api/weather/city/101230501',
					method: 'GET',
					success: (res) => {
						console.log("天气接口返回:", res.data);
						if (res.statusCode === 200) {
							const today = res.data.data.forecast[0];
							this.weatherResult = {
								city: res.data.cityInfo.city,
								date: today.date,
								type: today.type,
								tempRange: `${today.low} ~ ${today.high}`
							};
						} else {
							this.weatherResult = null;
							uni.showToast({
								title: '获取天气失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						this.weatherResult = null;
						console.log('注册请求失败:', err);
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					}
				})
			},
			downloadImage() {
				this.lastProgress = -1;
				uni.showLoading({
					title: '下载中...'
				});
				const imageTask = uni.downloadFile({
					url: this.imageUrl,
					timeout: 30000,
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode === 200) {
							console.log('------------------------');
							console.log('下载成功，临时路径:', res.tempFilePath);
							console.log('------------------------');
							this.iconFilePath = res.tempFilePath;
							uni.previewImage({
								urls: [res.tempFilePath],
								success: () => console.log('图片预览成功')
							});
						} else {
							console.error('------------------------');
							console.error('[图片下载] 失败，状态码:', res.statusCode);
							console.error('------------------------');
							uni.showToast({
								title: '下载失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.hideLoading();
						console.log('------------------------');
						console.log('下载失败:', err);
						console.log('------------------------');
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						});
					}
				});
				console.log('下载进度:', res.progress + "%");
				console.log('已下载:', res.totalBytesWritten);
				console.log('总大小:', res.totalBytesExpectedToWrite);
			}
		}
	}
</script>

<style>
	button {
		margin: 20rpx;
	}

	.tx {
		margin-bottom: 10%;
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		border: 2rpx solid #eee;
		object-fit: cover;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
	}
</style>